package com.ruicar.afs.cloud.channel.online.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.channel.channelworkflow.condition.ChannelOnlineWorkFlowCondition;
import com.ruicar.afs.cloud.channel.online.condition.ArchivesLogInfoCondition;
import com.ruicar.afs.cloud.channel.online.entity.ChannelArchivesInfo;
import com.ruicar.afs.cloud.channel.online.entity.ChannelBaseInfoTemp;
import com.ruicar.afs.cloud.channel.online.entity.ChannelRiskInfoTemp;
import com.ruicar.afs.cloud.channel.online.enums.ChannelOnlineEnum;
import com.ruicar.afs.cloud.channel.online.mapper.ChannelArchivesInfoMapper;
import com.ruicar.afs.cloud.channel.online.service.ChannelArchivesInfoService;
import com.ruicar.afs.cloud.channel.online.service.ChannelOnlineService;
import com.ruicar.afs.cloud.channel.online.service.ChannelRiskInfoService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruicar.afs.cloud.channel.online.untils.ChannelOnlineConstants.*;

/**
 * @author mingzhi.li
 * @version 1.0
 * @describe 渠道渠道建档流水表
 * @date 2020/06/5 11：20
 */
@Service
@AllArgsConstructor
public class ChannelArchivesInfoServiceImpl extends ServiceImpl<ChannelArchivesInfoMapper, ChannelArchivesInfo> implements ChannelArchivesInfoService {

    private final ChannelRiskInfoService channelRiskInfoService;
    private final ChannelOnlineService channelOnlineService;

    @Override
    public void saveArchivesInfo(ChannelOnlineWorkFlowCondition condition,String businessType) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ChannelBaseInfoTemp infoTemp = channelOnlineService.getOne(Wrappers.<ChannelBaseInfoTemp>query().lambda().eq(ChannelBaseInfoTemp::getId, condition.getChannelId()));
        List<ChannelArchivesInfo> list = new ArrayList<>();
        if(BUSINESS_TYPE_NEW_CAR.equals(businessType)){
            //1.上线日期
            if(infoTemp !=null){
                ChannelArchivesInfo onlineDate = new ChannelArchivesInfo();
                onlineDate.setChannelId(infoTemp.getId());
                onlineDate.setEventName("新车上线日期");
                onlineDate.setDescription("新车上线日期:"+format.format(infoTemp.getOnlineDate()));
                list.add(onlineDate);
            }
            //2.保证金缴纳记录
            ChannelArchivesInfo onlineDeposit = new ChannelArchivesInfo();
            onlineDeposit.setChannelId(infoTemp.getId());
            onlineDeposit.setEventName("新车保证金缴纳记录");
            onlineDeposit.setDescription("新车保证金应缴金额:"+condition.getChannelDeposit()+";"+"实缴金额:"+condition.getChannelDepositPaidIn()+"日期:"+sdf.format(new Date()));
            list.add(onlineDeposit);

            //3.渠道评级
            ChannelRiskInfoTemp riskInfoTemp = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfoTemp>query().lambda().eq(ChannelRiskInfoTemp::getChannelId, infoTemp.getId().toString())
                    .eq(ChannelRiskInfoTemp::getBusinessType, BUSINESS_TYPE_NEW_CAR));
            if(riskInfoTemp!=null){
                ChannelArchivesInfo onlineGrade = new ChannelArchivesInfo();
                onlineGrade.setChannelId(infoTemp.getId());
                onlineGrade.setEventName("新车渠道评级");
                onlineGrade.setDescription("新车渠道评级:"+setInfo(riskInfoTemp.getChannelGrade()));
                list.add(onlineGrade);
            }
            this.saveBatch(list);
        }else{
            //1.上线日期
            if(infoTemp !=null){
                ChannelArchivesInfo onlineDate = new ChannelArchivesInfo();
                onlineDate.setChannelId(infoTemp.getId());
                onlineDate.setEventName("二手车上线日期");
                onlineDate.setDescription("二手车上线日期:"+format.format(infoTemp.getOnlineDate()));
                list.add(onlineDate);
            }
            //2.保证金缴纳记录
            ChannelArchivesInfo onlineDeposit = new ChannelArchivesInfo();
            onlineDeposit.setChannelId(infoTemp.getId());
            onlineDeposit.setEventName("二手车保证金缴纳记录");
            onlineDeposit.setDescription("二手车保证金应缴金额:"+condition.getChannelDeposit()+";"+"实缴金额:"+condition.getChannelDepositPaidIn()+"日期:"+sdf.format(new Date()));
            list.add(onlineDeposit);

            //3.渠道评级
            ChannelRiskInfoTemp riskInfoTemp = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfoTemp>query().lambda().eq(ChannelRiskInfoTemp::getChannelId, infoTemp.getId().toString())
                    .eq(ChannelRiskInfoTemp::getBusinessType, BUSINESS_TYPE_OLD_CAR));
            if(riskInfoTemp!=null){
                ChannelArchivesInfo onlineGrade = new ChannelArchivesInfo();
                onlineGrade.setChannelId(infoTemp.getId());
                onlineGrade.setEventName("二手车渠道评级");
                onlineGrade.setDescription("二手车渠道评级:"+setInfo(riskInfoTemp.getChannelGrade()));
                list.add(onlineGrade);
            }
            this.saveBatch(list);
        }
    }

    public String setInfo(String grade) {
        if(AfsEnumUtil.key(ChannelOnlineEnum.GRADE_A).equals(grade)){
            grade=AfsEnumUtil.desc(ChannelOnlineEnum.GRADE_A);
        }else if(AfsEnumUtil.key(ChannelOnlineEnum.GRADE_B).equals(grade)){
            grade=AfsEnumUtil.desc(ChannelOnlineEnum.GRADE_B);
        }else if(AfsEnumUtil.key(ChannelOnlineEnum.GRADE_C).equals(grade)){
            grade=AfsEnumUtil.desc(ChannelOnlineEnum.GRADE_C);
        }else{
            grade=AfsEnumUtil.desc(ChannelOnlineEnum.GRADE_D);
        }
        return grade;
    }

    /*保存额度修改日志信息*/
    @Override
    public void saveQuotaChangeArchivesInfo(ArchivesLogInfoCondition condition) {
        Assert.notNull(condition.getChannelId(),"渠道id不能为空");
        Assert.notNull(condition.getEventName(),"事件名称不能为空");
        Assert.notNull(condition.getDescription(),"详细信息不能为空");
        ChannelArchivesInfo channelArchivesInfo = new ChannelArchivesInfo();
        channelArchivesInfo.setChannelId(condition.getChannelId());
        channelArchivesInfo.setEventName(condition.getEventName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        channelArchivesInfo.setDescription(condition.getDescription()+"   " + "操作日期:" + sdf.format(new Date()));
        save(channelArchivesInfo);
    }

    /*更新渠道评级日志信息*/
    @Override
    public void saveUpdateGradeArchivesInfo(ArchivesLogInfoCondition condition) {
        Assert.notNull(condition.getChannelId(), "渠道id不能为空");
        Assert.notNull(condition.getEventName(), "事件名称不能为空");
        Assert.notNull(condition.getDescription(), "详细信息不能为空");
        ChannelArchivesInfo channelArchivesInfo = new ChannelArchivesInfo();
        channelArchivesInfo.setChannelId(condition.getChannelId());
        channelArchivesInfo.setEventName(condition.getEventName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        channelArchivesInfo.setDescription(condition.getDescription() + "   " + "操作日期:" + sdf.format(new Date()));
        save(channelArchivesInfo);
    }

}
